using System;
using Cysharp.Threading.Tasks;

namespace Processors
{
    /// <summary>
    /// 处理器基类
    /// </summary>
    /// <typeparam name="TInput">Input数据类型</typeparam>
    /// <typeparam name="TOutput">Output数据类型</typeparam>
    public abstract class ProcessorBase<TInput, TOutput> : IProcessor
    {
        public async UniTask<object> Process(object input, Context context)
        {
            try
            {
                return await Process((TInput)input, context);
            }
            catch (InvalidCastException)
            {
                throw new Exception($"processor({GetType()}) input不匹配, 输入的类型为{input.GetType()}");
            }
            catch (Exception e)
            {
                // todo 使用ILogger<T>写法替换Console.WriteLine
                // Debug.LogException(e);
                Console.WriteLine(e.ToString());
                // Debug.LogError($"{this.GetType()}:{e.Message}");
                throw ;
            }
        }

        protected abstract UniTask<TOutput> Process(TInput input, Context context);
    }
}